// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.common.controller;

import com.liuxinlong.common.AosException;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.common.controller.request.BaseReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.request.QueryWorkReq;
import com.liuxinlong.modules.common.controller.request.QueryWorkflowReq;
import com.liuxinlong.modules.common.controller.request.UpdateWorkflowReq;
import com.liuxinlong.modules.common.controller.request.UrgingWorkflowReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.common.service.WorkCenterService;
import com.liuxinlong.modules.common.service.WorkFlowService;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作中心控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/work")
public class WorkCenterController {

    @Autowired
    private WorkCenterService workCenterService;

    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 查询工作中心信息
     *
     * @return 工作中心信息
     */
    @GetMapping("query")
    public BaseResp queryTotalInfo() {
        Map<String, Object> resultMap = workCenterService.queryTotalInfo();
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("查询工作中心信息成功！");
        return resp;
    }

    /**
     * 查询待办信息
     *
     * @return 待办信息
     */
    @PostMapping("need")
    public BaseResp queryNeedList(@RequestBody QueryWorkReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("sourceType", req.getSourceType());
        List<Map<String, Object>> dataList = workCenterService.queryNeedList(queryParam);
//        int total = workCenterService.getWorkCount();
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
//        resp.setTotalNum(total);
        resp.setRetMessage("查询待办信息成功！");
        return resp;
    }

    /**
     * 查询已办信息
     *
     * @return 已办信息
     */
    @PostMapping("done")
    public BaseResp queryDoneList(@RequestBody QueryWorkReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("sourceType", req.getSourceType());
        List<Map<String, Object>> dataList = workCenterService.queryDoneList(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询已办信息成功！");
        return resp;
    }

    /**
     * 查询动态信息
     *
     * @return 动态信息
     */
    @PostMapping("trends")
    public BaseResp queryTrendsList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryParam.put("dealUser", current.getId());
        List<Map<String, Object>> dataList = workCenterService.queryTrendsList(queryParam);
        int total = workCenterService.getTrendsCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询动态信息成功！");
        return resp;
    }

    /**
     * 查询关注用户列表
     *
     * @return 关注用户列表
     */
    @PostMapping("follow")
    public BaseResp queryFollowList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        if (ObjectUtils.isEmpty(current)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"用户不存在！");
        }
        queryParam.put("userId", current.getId());
        List<Map<String, Object>> dataList = workCenterService.queryFollowList(queryParam);
        int total = workCenterService.getFollowCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询关注用户列表成功！");
        return resp;
    }

    /**
     * 查询粉丝用户列表
     *
     * @return 粉丝用户列表
     */
    @PostMapping("fans")
    public BaseResp queryFansList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        if (ObjectUtils.isEmpty(current)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"用户不存在！");
        }
        queryParam.put("userId", current.getId());
        List<Map<String, Object>> dataList = workCenterService.queryFansList(queryParam);
        int total = workCenterService.getFansCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询粉丝用户列表成功！");
        return resp;
    }

    /**
     * 查询我的申请信息
     *
     * @return 我的申请信息
     */
    @PostMapping("workflow-apply")
    public BaseResp queryWorkflowApplyList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryParam.put("createUser", current.getId());
        List<Map<String, Object>> dataList = workCenterService.queryWorkflowApplyList(queryParam);
        int total = workCenterService.getWorkflowApplyCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询我的申请信息成功！");
        return resp;
    }

    /**
     * 查询抄送我的信息
     *
     * @return 抄送我的信息
     */
    @PostMapping("workflow-cc")
    public BaseResp queryWorkflowCCList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryParam.put("createUser", current.getId());
        List<Map<String, Object>> dataList = workCenterService.queryWorkflowCCList(queryParam);
        int total = workCenterService.getWorkflowCCCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询我的申请信息成功！");
        return resp;
    }

    /**
     * 查询全部流程信息
     *
     * @return 全部流程信息
     */
    @PostMapping("workflow-all")
    public BaseResp queryWorkflowAllList(@RequestBody BaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = workCenterService.queryWorkflowAllList(queryParam);
        int total = workCenterService.getWorkflowAllCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        resp.setRetMessage("查询全部流程信息成功！");
        return resp;
    }

    /**
     * 流程数据详情
     *
     * @return 成功标识
     */
    @PostMapping("workflow-detail")
    public BaseResp queryWorkflowDetail(@RequestBody QueryWorkflowReq req) {
        Map<String,Object> dataMap = workFlowService.queryWorkflowDetail(req.getId(),req.getChildId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("流程数据详情查询成功！");
        return resp;
    }

    /**
     * 流程处理
     *
     * @return 成功标识
     */
    @PostMapping("workflow-deal")
    public BaseResp dealWorkflow(@RequestBody UpdateWorkflowReq req) {
        workFlowService.dealWorkflow(req.getId(),req.getResult(),req.getDealOpinions());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("流程处理成功！");
        return resp;
    }

    /**
     * 流程催办
     *
     * @return 成功标识
     */
    @PostMapping("workflow-urging")
    public BaseResp urgingWorkflow(@RequestBody UrgingWorkflowReq req) {
        workFlowService.urgingWorkflow(req.getId(),req.getTargetUser());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("流程催办成功！");
        return resp;
    }

    /**
     * 流程撤回
     *
     * @return 成功标识
     */
    @PostMapping("workflow-recall")
    public BaseResp recallWorkflow(@RequestBody QueryBaseReq req) {
        workFlowService.recallWorkflow(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("流程撤回成功！");
        return resp;
    }
}
